﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace X3DLib
{
    #region //重构后的树类
    public class Tree : X3dInterfaceObject
    {
        //public Group globalGroup;//用于保存第二个Group，提高设置属性时的速度

        private int demoIndex = 1;//小草模型

        /// <summary>
        /// 选择小草模型的序号（1或2）
        /// </summary>
        private int DemoIndex
        {
            get { return demoIndex; }
            set { demoIndex = value; }
        }

        private string leafNum;

        /// <summary>
        /// 叶子数目
        /// </summary>
        public string LeafNum
        {
            get { return leafNum; }
            set { leafNum = value; }
        }

        private string leafColor;
        /// <summary>
        /// 叶子颜色
        /// </summary>
        public string LeafColor
        {
            get { return leafColor; }
            set { leafColor = value; }
        }

        private string leafAngle;
        /// <summary>
        /// 叶子的角度
        /// </summary>
        public string LeafAngle
        {
            get { return leafAngle; }
            set { leafAngle = value; }
        }

        private string trunkColor;
        /// <summary>
        /// 树干颜色
        /// </summary>
        public string TrunkColor
        {
            get { return trunkColor; }
            set { trunkColor = value; }
        }

        private string trunkAngle;
        /// <summary>
        /// 树干的角度
        /// </summary>
        public string TrunkAngle
        {
            get { return trunkAngle; }
            set { trunkAngle = value; }
        }

        private string trunkHeight;
        /// <summary>
        /// 树干的高度
        /// </summary>
        public string TrunkHeight
        {
            get { return trunkHeight; }
            set { trunkHeight = value; }
        }

        private string iterateNum;
        /// <summary>
        /// 迭代次数
        /// </summary>
        public string IterateNum
        {
            get { return iterateNum; }
            set { iterateNum = value; }
        }

        /// <summary>
        /// 构造函数，默认使用第一个模型，迭代2次。
        /// </summary>
        public Tree()
        {
            this.demoIndex = 1;
            this.leafNum = "3";
            this.leafColor = "0 1 0";
            this.leafAngle = "0.785";
            this.trunkColor = "0.5 0.4 0.3";
            this.trunkAngle = "1.57";
            this.trunkHeight = "3";
            this.iterateNum = "2";

            int iteratetime = Int32.Parse(this.iterateNum);
            //setScene(setGroup(1,"0 1 0","2"),1);

            //this.addChildren(setLeaf());

            this.addChildren(setGroup());
            Group[] groupArr = new Group[iteratetime];
            groupArr = setGroupArr();
            foreach (Group tempgroup in groupArr)
            {
                this.addChildren(tempgroup);
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="demoIndex">指定树木模型序号</param>
        /// <param name="iterateNum">指定迭代深度</param>
        public Tree(int demoIndex, string iterateNum)
        {
            this.demoIndex = demoIndex;
            this.leafNum = "3";
            this.leafColor = "0 1 0";
            this.leafAngle = "0.785";
            this.trunkColor = "0.5 0.4 0.3";
            this.trunkAngle = "1.57";
            this.trunkHeight = "3";
            this.iterateNum = iterateNum;

            int iteratetime = Int32.Parse(this.iterateNum);
            //setScene(setGroup(demoIndex, "0 1 0", "2"), deep);

            //this.addChildren(setLeaf());

            this.addChildren(setGroup());
            Group[] groupArr = new Group[iteratetime];
            groupArr = setGroupArr();
            foreach (Group tempgroup in groupArr)
            {
                this.addChildren(tempgroup);
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="demoIndex">树木模型，eg：1，2</param>
        /// <param name="leafNum">树叶数目：eg：3</param>
        /// <param name="leafColor">树叶颜色，eg：0 1 0</param>
        /// <param name="leafAngle">树叶角度：eg：0.785</param>
        /// <param name="trunkColor">树干颜色，eg：0.5 0.4 0.3</param>
        /// <param name="trunkAngle">树干角度，eg：1.57</param>
        /// <param name="trunkHeight">树干高度，eg：3</param>
        /// <param name="iterateNum">迭代次数，eg：2</param>
        public Tree(int demoIndex, string leafNum, string leafColor, string leafAngle, string trunkColor, string trunkAngle, string trunkHeight, string iterateNum)
        {
            //setScene(setGroup(demoIndex, color, size), deep);
            this.demoIndex = demoIndex;
            this.leafNum = leafNum;
            this.leafColor = leafColor;
            this.leafAngle = leafAngle;
            this.trunkColor = trunkColor;
            this.trunkAngle = trunkAngle;
            this.trunkHeight = trunkHeight;
            this.iterateNum = iterateNum;

            //this.addChildren(setLeaf());

            int iteratetime = Int32.Parse(this.iterateNum);
            this.addChildren(setGroup());
            Group[] groupArr = new Group[iteratetime];
            groupArr = setGroupArr();
            foreach (Group tempgroup in groupArr)
            {
                this.addChildren(tempgroup);
            }
        }
        /// <summary>
        /// 设置树叶
        /// </summary>
        /// <returns></returns>
        public Group setLeaf()
        {
            Group group = new Group();
            group.DEF = "L0";
            #region TreeDemo 1 / 2
            if (this.demoIndex == 1 || this.demoIndex == 2)
            {
                //第一片叶子
                Transform transform = new Transform();
                transform.DEF = "leaf";
                //transform.rotation = "1 0 0 0.785";
                transform.center = "0 -0.2 0";

                Shape leafShape = new Shape();
                //tempShape1.DEF = "L0";
                Coordinate tempCoor1;
                if (this.demoIndex == 1)
                {
                    tempCoor1 = new Coordinate("0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2");
                }
                else
                {
                    tempCoor1 = new Coordinate("0 " + (double.Parse(this.trunkHeight)) + " 0 0.4 " + (double.Parse(this.trunkHeight) + 0.6) + " 0.2 0.8 " + (double.Parse(this.trunkHeight) + 1.2) + " 0 0.2 " + (double.Parse(this.trunkHeight) + 0.6) + " -0.2");
                }

                IndexedFaceSet faceSetLeaf = new IndexedFaceSet();
                faceSetLeaf.coordIndex = "0 1 2 3";
                faceSetLeaf.solid = "false";
                faceSetLeaf.Coordinate = tempCoor1;

                Appearance tempApp1 = new Appearance();
                tempApp1.material = new Material(this.leafColor);//color="0 1 0"

                leafShape.geometry = faceSetLeaf;
                leafShape.appearance = tempApp1;

                transform.addChildren(leafShape);
                group.addChildren(transform);

                int templeafnum = Int32.Parse(this.leafNum);
                X3dScaleRandom scalerandom = new X3dScaleRandom(1.0f, 1.0f, 1.0f, templeafnum);
                string[] scaleArr = scalerandom.createScaleArr();

                float[] angles = new float[templeafnum];
                Transform[] tranArr = new Transform[templeafnum];
                //其他叶子
                for (int i = 0; i < templeafnum-1; i++)
                {
                    angles[i] = (float)(6.282 / templeafnum * (i + 1));
                    tranArr[i] = new Transform();
                    tranArr[i].scale = scaleArr[i];
                    tranArr[i].rotation = "0 1 0 " + angles[i].ToString();
                    tranArr[i].center = "0 -0.2 0";
                    tranArr[i].addChildren(new Shape("leaf"));

                    group.addChildren(tranArr[i]);
                }
                //树叶枝干
                Shape leafTrunkShape = new Shape();
                leafTrunkShape.DEF = "";
                IndexedLineSet indexedLineSet = new IndexedLineSet();
                indexedLineSet.coordIndex = "0 1";
                Coordinate tempCoor2 = new Coordinate("0 0 0 0 1 0");//size="0 0 0 0 2 0"
                indexedLineSet.Coordinate = tempCoor2;

                Appearance tempApp2 = new Appearance();
                tempApp2.material.emissiveColor = "0.5 0.5 0";
                leafTrunkShape.appearance = tempApp2;
                leafTrunkShape.geometry = indexedLineSet;

                group.addChildren(leafTrunkShape);
                return group;

            }
            #endregion
            #region TreeDemo 3
            else if (this.demoIndex == 3)
            {
                //第一片叶子
                Transform transform = new Transform();
                transform.DEF = "leaf";
                transform.center = "0 -0.2 0";

                Shape leafShape = new Shape();
                //if (this.demoIndex == 3 || this.demoIndex ==4)
                {
                    //tempShape1.DEF = "L0";
                    Coordinate tempCoor1 = new Coordinate("0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2");

                    IndexedFaceSet faceSetLeaf = new IndexedFaceSet();
                    faceSetLeaf.coordIndex = "0 1 2 3";
                    faceSetLeaf.solid = "false";
                    faceSetLeaf.Coordinate = tempCoor1;

                    Appearance tempApp1 = new Appearance();
                    tempApp1.material = new Material(this.leafColor);//color="0 1 0"

                    leafShape.geometry = faceSetLeaf;
                    leafShape.appearance = tempApp1;
                }
                //else if (this.demoIndex == 4)
                //{
                //    transform.scale = "0.2 1.1 0.05";
                //    transform.translation = "0 4 0";
                //    transform.rotation = "1 0 0 " + this.leafAngle;//"1 0 0 0.785";
                //    Sphere sphere = new Sphere();
                //    sphere.radius = "0.5";
                //    leafShape.addChildren(sphere);

                //    Appearance app = new Appearance();
                //    app.material = new Material(this.leafColor);
                //    leafShape.addChildren(app);
                //}
                transform.addChildren(leafShape);
                group.addChildren(transform);

                int templeafnum = Int32.Parse(this.leafNum);
                //X3dScaleRandom scalerandom = new X3dScaleRandom(1.0f, 1.0f, 1.0f, templeafnum);
                //string[] scaleArr = scalerandom.createScaleArr();

                float[] angles = new float[templeafnum];
                Transform[] tranArr = new Transform[templeafnum];
                //其他叶子
                for (int i = 0; i < templeafnum-1; i++)
                {
                    angles[i] = (float)(6.282 / templeafnum * (i + 1));
                    tranArr[i] = new Transform();
                    //tranArr[i].scale = scaleArr[i];
                    tranArr[i].rotation = "0 1 0 " + angles[i].ToString();
                    tranArr[i].center = "0 -0.2 0";
                    tranArr[i].addChildren(new Shape("leaf"));
                    group.addChildren(tranArr[i]);
                }
                return group;

            }

            #endregion
            #region TreeDemo 4
            else if (this.demoIndex == 4)
            {
                //第一片叶子
                Transform transform = new Transform();
                transform.DEF = "leaf";
                //transform.rotation = "1 0 0 0.785";
                transform.center = "0 -0.2 0";

                Shape leafShape = new Shape();
                //tempShape1.DEF = "L0";
                Coordinate tempCoor1 = new Coordinate("0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2");

                IndexedFaceSet faceSetLeaf = new IndexedFaceSet();
                faceSetLeaf.coordIndex = "0 1 2 3";
                faceSetLeaf.solid = "false";
                faceSetLeaf.Coordinate = tempCoor1;

                Appearance tempApp1 = new Appearance();
                tempApp1.material = new Material(this.leafColor);//color="0 1 0"

                leafShape.geometry = faceSetLeaf;
                leafShape.appearance = tempApp1;

                transform.addChildren(leafShape);
                group.addChildren(transform);

                int templeafnum = Int32.Parse(this.leafNum);
                X3dScaleRandom scalerandom = new X3dScaleRandom(1.0f, 1.0f, 1.0f, templeafnum);
                string[] scaleArr = scalerandom.createScaleArr();

                float[] angles = new float[templeafnum];
                Transform[] tranArr = new Transform[templeafnum];
                //其他叶子
                for (int i = 0; i < templeafnum-1; i++)
                {
                    angles[i] = (float)(6.282 / templeafnum * (i + 1));
                    tranArr[i] = new Transform();
                    tranArr[i].scale = scaleArr[i];
                    tranArr[i].rotation = "0 1 0 " + angles[i].ToString();
                    tranArr[i].center = "0 -0.2 0";
                    tranArr[i].addChildren(new Shape("leaf"));

                    group.addChildren(tranArr[i]);
                }
                //树叶枝干
                Shape leafTrunkShape = new Shape();
                leafTrunkShape.DEF = "";
                IndexedLineSet indexedLineSet = new IndexedLineSet();
                indexedLineSet.coordIndex = "0 1";
                Coordinate tempCoor2 = new Coordinate("0 0 0 0 1 0");//size="0 0 0 0 2 0"
                indexedLineSet.Coordinate = tempCoor2;

                Appearance tempApp2 = new Appearance();
                tempApp2.material.emissiveColor = "0.5 0.5 0";
                leafTrunkShape.appearance = tempApp2;
                leafTrunkShape.geometry = indexedLineSet;

                group.addChildren(leafTrunkShape);

                Transform bigTransform = new Transform();
                bigTransform.translation = "0 2 0";
                bigTransform.rotation = "0 0 1 -0.785";

                bigTransform.addChildren(group);

                Group bigGroup = new Group();
                bigGroup.addChildren(bigTransform);
                return bigGroup;
            }
            #endregion
            #region TreeDemo 5
            else if (this.demoIndex == 5)
            {
                //第一片叶子
                Transform transform = new Transform();
                //transform.DEF = "leafGroup";
                transform.rotation = "0 1 1 1.57";
                //transform.center = "0 -0.2 0";
                transform.translation = "-1 3.1 0.72";

                //<Transform center="0 -0.2 0" rotation="0 0 1 -0.785" scale="1 1 1" translation="0.13 0 0" DEF="L1" >

                Transform leafTran = new Transform();
                leafTran.DEF = "aleaf";
                leafTran.center = "0 -0.2 0";
                leafTran.rotation = "0 0 1 0.785";
                leafTran.scale = "0.8 0.9 0.2";

                Shape leafShape = new Shape();
                _Cone cone = new _Cone();
                cone.bottomRadius = "0.1";
                cone.height = "1";

                Appearance tempApp1 = new Appearance();
                tempApp1.material = new Material(this.leafColor);//color="0 1 0"

                leafShape.geometry = cone;
                leafShape.appearance = tempApp1;

                leafTran.addChildren(leafShape);
                transform.addChildren(leafTran);

                //Transform leafTran2 = new Transform();
                //leafTran2.center = "0 -0.2 0"; ;
                //leafTran2.rotation = "0 0 1 -0.785";
                //leafTran2.translation = "0.13 0 0";                

                int templeafnum = Int32.Parse(this.leafNum);
                //X3dScaleRandom scalerandom = new X3dScaleRandom(1.0f, 1.0f, 1.0f, templeafnum);
                //string[] scaleArr = scalerandom.createScaleArr();

                //float[] angles = new float[templeafnum];
                Transform[] tranArr = new Transform[templeafnum];
                //其他叶子
                for (int i = 0; i < templeafnum-1; i++)
                {
                    //angles[i] = (float)(3.141 / templeafnum * (i + 1));
                    tranArr[i] = new Transform();
                    tranArr[i].scale = "1 1 1";
                    tranArr[i].rotation = "0 0 1 -" + 0.785*(i+1);
                    tranArr[i].center = "0 -0.2 0";
                    tranArr[i].translation = (0.13+i/10.0)+" 0 0";
                    tranArr[i].addChildren(new Shape("aleaf"));

                    transform.addChildren(tranArr[i]);
                }
                group.addChildren(transform);
                //树叶枝干
                Transform trunkTran = new Transform();
                trunkTran.translation = "-0.7 3 0.4";
                trunkTran.scale = "0.25 1 0.25";
                trunkTran.rotation = "0 1 1 1.57";

                Shape leafTrunkShape = new Shape();
                leafTrunkShape.DEF = "LeafBanch";

                _Cone trunkCone = new _Cone();
                trunkCone.bottomRadius = "0.2";
                trunkCone.height = "2";

                Appearance tempApp2 = new Appearance();
                tempApp2.material = new Material("1 0.9 0.2");
                leafTrunkShape.appearance = tempApp2;
                leafTrunkShape.geometry = trunkCone;

                trunkTran.addChildren(leafTrunkShape);

                group.addChildren(trunkTran);

                return group;
            }
            #endregion
            #region TreeDemo 6
            if (this.demoIndex == 6)
            {
                //第一片叶子
                Shape leafShape = new Shape();
                leafShape.DEF = "leaf";
                Coordinate tempCoor1 = new Coordinate("0 " + (double.Parse(this.trunkHeight)) + " 0 0.4 " + (double.Parse(this.trunkHeight) + 0.6) + " 0.2 0.8 " + (double.Parse(this.trunkHeight) + 1.2) + " 0 0.2 " + (double.Parse(this.trunkHeight)+0.6) + " -0.2");

                IndexedFaceSet faceSetLeaf = new IndexedFaceSet();
                faceSetLeaf.coordIndex = "0 1 2 3";
                faceSetLeaf.solid = "false";
                faceSetLeaf.Coordinate = tempCoor1;

                Appearance tempApp1 = new Appearance();
                tempApp1.material = new Material(this.leafColor);//color="0 1 0"

                leafShape.geometry = faceSetLeaf;
                leafShape.appearance = tempApp1;

                //transform.addChildren(leafShape);
                group.addChildren(leafShape);

                int templeafnum = Int32.Parse(this.leafNum);

                float[] angles = new float[templeafnum];
                Transform[] tranArr = new Transform[templeafnum];
                //其他叶子
                for (int i = 0; i < templeafnum - 1; i++)
                {
                    angles[i] = (float)(6.282 / templeafnum * (i + 1));
                    tranArr[i] = new Transform();
                    tranArr[i].rotation = "0 1 0 " + angles[i].ToString();
                    tranArr[i].addChildren(new Shape("leaf"));

                    group.addChildren(tranArr[i]);
                }
                return group;

            }
            #endregion
            else
            {
                group.addChildren(new Shape(new Appearance(new Material("0 1 0")), new Box()));
                return group;
            }

        }

        /// <summary>
        /// 设置树干
        /// </summary>
        /// <returns></returns>
        public Group setTrunk()
        {
            Group group = new Group();
            Shape leafTrunkShape = new Shape();
            //树干1 2 6
            if (this.demoIndex == 1 || this.demoIndex == 2 || this.demoIndex==6)
            {
                leafTrunkShape.DEF = "";
                IndexedLineSet indexedLineSet = new IndexedLineSet();                
                indexedLineSet.coordIndex = "0 1";
                Coordinate tempCoor2 = new Coordinate("0 0 0 0 " + this.trunkHeight + " 0");//size="0 0 0 0 2 0"
                indexedLineSet.Coordinate = tempCoor2;

                Appearance tempApp = new Appearance();
                tempApp.material.emissiveColor = this.trunkColor;//"0.5 0.5 0";
                leafTrunkShape.appearance = tempApp;
                leafTrunkShape.geometry = indexedLineSet;

                group.addChildren(leafTrunkShape);
            }
            else if (this.demoIndex == 3 || this.demoIndex == 4)
            {
                leafTrunkShape.DEF = "Banch";

                IndexedFaceSet faceSetBanch = new IndexedFaceSet();
                faceSetBanch.DEF = "Trunk-FACES";
                faceSetBanch.ccw = "true";
                faceSetBanch.coordIndex = "0 1 9 8 -1,1 2 10 9 -1,2 3 11 10 -1,3 4 12 11 -1," +
                     "  4 5 13 12 -1,5 6 14 13 -1,6 7 15 14 -1,7 0 8 15 -1," +
                     "  0 1 2 3 4 5 6 7 -1,8 9 10 11 12 13 14 15";
                Coordinate tempCoor2 = new Coordinate();
                tempCoor2.DEF = "Trunk-COORD";
                tempCoor2.point = "0.1 0 0.2 0.2 0 0.1 0.2 0 -0.1 0.1 0 -0.2 -0.1 0 -0.2 -0.2 0 -0.1 -0.2 0 0.1" +
                     "  -0.1 0 0.2 0.05 " + this.trunkHeight + " 0.1 0.1 " + this.trunkHeight + " 0.05 0.1 " + this.trunkHeight + " -0.05 0.05 " + this.trunkHeight + " -0.1 -0.05 " + this.trunkHeight + " -0.1" +
                     "  -0.1 " + this.trunkHeight + " -0.05 -0.1 " + this.trunkHeight + " 0.05 -0.05 " + this.trunkHeight + " 0.1";
                faceSetBanch.Coordinate = tempCoor2;

                Appearance tempApp = new Appearance();
                tempApp.material.ambientIntensity = "1";
                tempApp.material.diffuseColor = this.trunkColor;
                tempApp.material.shininess = "0.525";
                tempApp.material.specularColor = "0 0 0";
                tempApp.material.transparency = "0";
                leafTrunkShape.appearance = tempApp;
                leafTrunkShape.geometry = faceSetBanch;
                group.addChildren(leafTrunkShape);

                //树干分支1
                Transform transformB1 = new Transform();
                transformB1.rotation = "-1 1 0.5 1.57";// + this.trunkAngle;
                transformB1.scale = "0.5 0.5 0.5";
                transformB1.translation = "0 1 0";
                transformB1.addChildren(new Shape(leafTrunkShape.DEF));
                group.addChildren(transformB1);
                //树干分支2
                Transform transformB2 = new Transform();
                transformB2.rotation = "1 1 0.5 -1.57";// + this.trunkAngle;
                transformB2.scale = "0.5 0.5 0.5";
                transformB2.translation = "0 2 0";
                transformB2.addChildren(new Shape(leafTrunkShape.DEF));
                group.addChildren(transformB2);
            }
            else if (this.demoIndex == 5)
            {
                Group trunkGroup = new Group();
                trunkGroup.DEF = "trunkGroup0";

                Transform nodeTran = new Transform();
                nodeTran.DEF = "Node0";
                nodeTran.scale = "1.1 0.2 1.1";

                Shape nodeShape = new Shape();

                Appearance nodeApp = new Appearance();
                nodeApp.material = new Material("0.5 0.4 0.3");

                nodeShape.appearance = nodeApp;
                nodeShape.geometry = new Sphere("0.2");

                nodeTran.addChildren(nodeShape);
                trunkGroup.addChildren(nodeTran);

                Shape banchShape = new Shape();
                banchShape.DEF = "Banch";

                Appearance banchApp = new Appearance();
                Material material = new Material(this.trunkColor);
                material.specularColor = "0 0 0";
                material.shininess = "0.525";
                material.ambientIntensity = "1";
                material.transparency = "0";

                banchApp.material = material;

                IndexedFaceSet faceSetBanch = new IndexedFaceSet();
                faceSetBanch.DEF = "Trunk-FACES";
                faceSetBanch.ccw = "true";
                faceSetBanch.coordIndex = "0 1 9 8 -1,1 2 10 9 -1,2 3 11 10 -1,3 4 12 11 -1," +
                     "  4 5 13 12 -1,5 6 14 13 -1,6 7 15 14 -1,7 0 8 15 -1," +
                     "  0 1 2 3 4 5 6 7 -1,8 9 10 11 12 13 14 15";
                Coordinate tempCoor2 = new Coordinate();
                tempCoor2.DEF = "Trunk-COORD";
                tempCoor2.point = "0.075 0 0.15 0.15 0 0.075 0.15 0 -0.075 0.075 0 -0.15 -0.075 0 -0.15 -0.15 0 -0.075 -0.15 0 0.075  -0.075 0 0.15"
                    +" 0.05 " + this.trunkHeight + " 0.1 0.1 " + this.trunkHeight + " 0.05 0.1 " + this.trunkHeight + " -0.05 0.05 " + this.trunkHeight + " -0.1 -0.05 " + this.trunkHeight + " -0.1" +
                     "  -0.1 " + this.trunkHeight + " -0.05 -0.1 " + this.trunkHeight + " 0.05 -0.05 " + this.trunkHeight + " 0.1";
                faceSetBanch.Coordinate = tempCoor2;

                banchShape.appearance = banchApp;
                banchShape.geometry = faceSetBanch;
                trunkGroup.addChildren(banchShape);

                Transform otherTran = new Transform();
                otherTran.translation = "0 2 0";
                otherTran.rotation = "0 0 1 3.14";
                otherTran.addChildren(new Shape("Banch"));
                
                trunkGroup.addChildren(otherTran);

                group.addChildren(trunkGroup);

                Transform[] tranArr = new Transform[4];
                for (int i = 0; i < 3; i++)
                {
                    tranArr[i] = new Transform();
                    tranArr[i].DEF = "trunkGroup"+(i + 1);
                    tranArr[i].scale = "0.9 0.9 0.9";
                    tranArr[i].translation = "0 2 0";
                    tranArr[i].addChildren(new Group("trunkGroup" + i));
                    group.addChildren(tranArr[i]);
                }
 
            }
            return group;
        }

        /// <summary>
        /// 设置树木的基础模型
        /// </summary>
        /// <param name="demoIndex">指定树木模型序号</param>
        /// <param name="color">颜色</param>
        /// <param name="size">大小（高度）</param>
        /// <returns>Group</returns>
        public Group setGroup()
        {
            Group group = new Group();
            group.DEF = "group0";

            //Group demoGroup = new Group();
            //demoGroup.DEF = "demo";

            //Background background = new Background();
            Background background = new BackgroundCreater("day").GetBackground();
            //Background background = new BackgroundCreater("diy","skyColor|0.2 0.8 1,groundColor|0.5 0.4 0.3").GetBackground();
            //background.skyColor = "0.24 0.84 1";
            //background.groundColor = "0.5 0.5 0.5";
            group.addChildren(background);
            //第一片叶子
            group.addChildren(setLeaf());

            #region TreeDemo 1
            if (this.demoIndex == 1)
            {
                //第二片叶子
                Transform transform = new Transform();
                transform.rotation = "0 1 0 3.14";
                transform.translation = "0 1 0";
                transform.addChildren(new Shape("L0"));
                group.addChildren(transform);
                //树干
                //group.addChildren(setTrunk());
            }
            #endregion

            #region TreeDemo 2
            else if (demoIndex == 2)
            {
                //第二片叶子
                Transform transform = new Transform();
                transform.rotation = "0 1 0 3.14";
                transform.translation = "0 0 0";
                transform.addChildren(new Shape("leaf"));
                group.addChildren(transform);

                //第三片叶子
                transform = new Transform();
                transform.rotation = "0 1 0 1.57";
                transform.translation = "0 0 0";
                transform.addChildren(new Shape("leaf"));
                group.addChildren(transform);
            }
            #endregion

            #region TreeDemo 3
            else if (demoIndex == 3)
            {
                //第二片叶子
                Transform transformL1 = new Transform();
                transformL1.rotation = "0 1 0 3.14";
                transformL1.translation = "0 2 0";
                transformL1.addChildren(new Shape("L0"));
                group.addChildren(transformL1);
                //第三片叶子
                Transform transformL2 = new Transform();
                transformL2.rotation = "0 1 0 1.57";
                transformL2.translation = "0 2 0";
                transformL2.addChildren(new Shape("L0"));
                group.addChildren(transformL2);
                //第四片叶子
                Transform transformL3 = new Transform();
                transformL3.rotation = "0 0 1 0.785";//"0 1 0 3.14";
                transformL3.scale = "0.5 0.5 0.5";
                transformL3.translation = "-0.75 1.199 -0.68";//"-0.7 1 -0.38";
                transformL3.addChildren(new Shape("L0"));
                group.addChildren(transformL3);
                //第五片叶子
                Transform transformL4 = new Transform();
                transformL4.rotation = "0 0 1 0.785";//"0 1 0 1.57";
                transformL4.scale = "0.4 0.4 0.4";
                transformL4.translation = "-0.35 1.199 -0.5";// "-0.7 1 -0.38";
                transformL4.addChildren(new Shape("L0"));
                group.addChildren(transformL4);

            }
            #endregion
            #region TreeDemo 4
            else if (demoIndex == 4)
            {
                //////////////////////////////////////////////////////////////2010.5.1
                //第二片叶子
                Transform transformL1 = new Transform();
                transformL1.rotation = "1 0 0 0.785";
                transformL1.translation = "0 2 0";
                transformL1.addChildren(new Shape("L0"));
                group.addChildren(transformL1);
                //第三片叶子
                Transform transformL2 = new Transform();
                transformL2.rotation = "1 0 0 -0.785";
                transformL2.translation = "0 2 0";
                transformL2.addChildren(new Shape("L0"));
                group.addChildren(transformL2);
                //第四片叶子
                Transform transformL3 = new Transform();
                transformL3.rotation = "0 0 1 0.785";
                transformL3.scale = "0.5 0.5 0.5";
                transformL3.translation = "-0.7 1.3 -0.38";
                transformL3.addChildren(new Shape("L0"));
                group.addChildren(transformL3);
                //第5片叶子
                Transform transformL4 = new Transform();
                transformL4.rotation = "0 0 1 1.57";
                transformL4.scale = "0.5 0.5 0.5";
                transformL4.translation = "-0.7 1.5 -0.38";
                transformL4.addChildren(new Shape("L0"));
                group.addChildren(transformL4);
            }
            #endregion
            #region TreeDemo 5
            else if (demoIndex == 5)
            {
                //////////////////////////////////////////////////////////////2010.5.1
                //第二片叶子
                Transform transformL1 = new Transform();
                transformL1.rotation = "0 1 0 3.14";
                transformL1.translation = "0.145 3 -0.05";
                transformL1.addChildren(new Group("L0"));
                group.addChildren(transformL1);
                //第三片叶子
                Transform transformL2 = new Transform();
                transformL2.translation = "0 3.1 0";
                transformL2.addChildren(new Group("L0"));
                group.addChildren(transformL2);
                
                ////第四片叶子
                //Transform transformL3 = new Transform();
                //transformL3.rotation = "0 0 1 0.785";
                //transformL3.scale = "0.5 0.5 0.5";
                //transformL3.translation = "-0.7 1.3 -0.38";
                //transformL3.addChildren(new Shape("L0"));
                //group.addChildren(transformL3);
                ////第5片叶子
                //Transform transformL4 = new Transform();
                //transformL4.rotation = "0 0 1 1.57";
                //transformL4.scale = "0.5 0.5 0.5";
                //transformL4.translation = "-0.7 1.5 -0.38";
                //transformL4.addChildren(new Shape("L0"));
                //group.addChildren(transformL4);
            }
            #endregion
            #region TreeDemo 6
            else if (this.demoIndex == 6)
            {
                //树干
                //group.addChildren(setTrunk());
            }
            #endregion
            else
            {
                group.addChildren(new Shape(new Appearance(new Material("0 1 0")), new Box()));
            }
            //树干
            group.addChildren(setTrunk());
            return group;
        }

        /// <summary>
        /// 在第一个Group的基础上设置迭代后的树木Group
        /// </summary>
        /// <param name="deep">迭代次数</param>
        /// <returns>Group[]</returns>
        public Group[] setGroupArr()
        {
            int deep = Int32.Parse(this.iterateNum);
            Group[] groupArr = new Group[deep];

            if (this.demoIndex != 5)
            {
                for (int i = 0; i < deep; i++)
                {
                    groupArr[i] = new Group();
                    groupArr[i].DEF = "group" + (i + 1);
                    Transform[] transformArr = new Transform[5];
                    transformArr[0] = new Transform();
                    transformArr[0].DEF = "s" + (i + 1);
                    transformArr[0].scale = "0.5 0.5 0.5";
                    transformArr[0].addChildren(new Group("group" + i));

                    transformArr[1] = new Transform();
                    transformArr[1].translation = "0 1 0";
                    transformArr[1].addChildren(new Transform("s" + (i + 1)));

                    transformArr[2] = new Transform();
                    transformArr[2].DEF = "g" + (i + 1);
                    transformArr[2].rotation = "1 0 0 "+this.trunkAngle;//"1 0 0 0.75";
                    transformArr[2].translation = "0 1.7 0";// "0 1 0";// 
                    transformArr[2].addChildren(new Transform("s" + (i + 1)));

                    transformArr[3] = new Transform();
                    transformArr[3].rotation = "0 1 0 2.09";
                    transformArr[3].addChildren(new Transform("g" + (i + 1)));

                    transformArr[4] = new Transform();
                    transformArr[4].rotation = "0 1 0 -2.09";
                    transformArr[4].addChildren(new Transform("g" + (i + 1)));

                    foreach (Transform temp in transformArr)
                    {
                        groupArr[i].addChildren(temp);
                    }
                    //System.Windows.Forms.MessageBox.Show(groupArr[i].toString());
                }
            }
            else//this.demoIndex==5 //Bamboo
            {
                for (int i = 0; i < deep; i++)
                {
                    groupArr[i] = new Group();
                    groupArr[i].DEF = "group" + (i + 1);

                    Transform transform = new Transform();
                    transform = new Transform();
                    transform.DEF = "group" + (i + 1);
                    transform.rotation = "0 0 1 "+(0.1 +i/10.0);
                    transform.scale = "0.5 1.1 0.5";
                    transform.translation = (0.5 + i / 10.0) + " 0 0";
                    transform.addChildren(new Group("group" + i));

                    groupArr[i].addChildren(transform);
                }
            }
            return groupArr;
        }

        /// <summary>
        /// 功能相当于setGroupArr，区别是把所有的Group添加到Scene并返回
        /// </summary>
        /// <param name="group"></param>
        /// <param name="deep"></param>
        /// <returns></returns>
        public Scene setScene(Group group, int deep)
        {
            Scene scene = new Scene();
            scene.addChildren(group);

            for (int i = 1; i <= deep; i++)
            {
                Group tempgroup = new Group();
                tempgroup.DEF = "group" + i;
                Transform[] transformArr = new Transform[5];
                transformArr[0] = new Transform();
                transformArr[0].DEF = "s" + i;
                transformArr[0].scale = "0.5 0.5 0.5";
                transformArr[0].addChildren(new Group("group" + (i - 1)));

                transformArr[1] = new Transform();
                transformArr[1].translation = "0 1 0";
                transformArr[1].addChildren(new Transform("s" + i));

                transformArr[2] = new Transform();
                transformArr[2].DEF = "g" + i;
                transformArr[2].rotation = "1 0 0 0.75";
                transformArr[2].translation = "0 1.7 0";
                transformArr[2].addChildren(new Transform("s" + i));

                transformArr[3] = new Transform();
                transformArr[3].rotation = "0 1 0 2.09";
                transformArr[3].addChildren(new Transform("g" + i));

                transformArr[4] = new Transform();
                transformArr[4].rotation = "0 1 0 -2.09";
                transformArr[4].addChildren(new Transform("g" + i));

                foreach (Transform temp in transformArr)
                {
                    tempgroup.addChildren(temp);
                }

                scene.addChildren(tempgroup);

            }
            return scene;
        }

        /// <summary>
        /// 获取树木属性
        /// </summary>
        /// <returns></returns>
        public override string getAttrs()
        {
            return "demoIndex|" + this.demoIndex.ToString() + "|max:2 min:1,"
                    + "leafNum|" + this.leafNum + "|int32,"
                    + "leafColor|" + this.leafColor + "|max:1 min:0,"
                    + "leafAngle|" + this.leafAngle + "|float,"
                    + "trunkColor|" + this.trunkColor + "|max:1 min:0,"
                     + "trunkAngle|" + this.trunkAngle + "|max:1 min:0,"
                    + "trunkHeight|" + this.trunkHeight + "|float,"
                    + "iterateNum|" + this.iterateNum + "|int32";
        }

        /// <summary>
        /// 设置数目属性
        /// </summary>
        /// <param name="attrs">属性字符串，格式ver|ver|ver,…</param>
        public override void setAttrs(string attrs)
        {
            string[][] temp = X3DString.getArrayByAttrs(attrs);
            for (int i = 0; i < temp.Length; i++)
            {
                switch (temp[i][0])
                {
                    case "demoIndex":
                        this.demoIndex = Int32.Parse(temp[i][1]);
                        break;
                    case "leafNum":
                        this.leafNum = temp[i][1];
                        break;
                    case "leafColor":
                        this.leafColor = temp[i][1];
                        break;
                    case "leafAngle":
                        this.leafAngle = temp[i][1];
                        break;
                    case "trunkColor":
                        this.trunkColor = temp[i][1];
                        break;
                    case "trunkAngle":
                        this.trunkAngle = temp[i][1];
                        break;
                    case "trunkHeight":
                        this.trunkHeight = temp[i][1];
                        break;
                    case "iterateNum":
                        this.iterateNum = temp[i][1];
                        break;
                }
            }
            setAttributes(this.demoIndex, this.leafNum, this.leafColor, this.leafAngle, this.trunkColor, this.trunkAngle, this.trunkHeight, this.iterateNum);

        }

        /// <summary>
        /// 设置树木的属性，内部使用
        /// </summary>
        /// <param name="demoIndex">指定树木模型</param>
        /// <param name="color">树叶颜色</param>
        /// <param name="size">高度</param>
        private void setAttributes(int demoIndex, string leafNum, string leafColor, string leafAngle, string trunkColor, string trunkAngle, string trunkHeight, string iterateNum)
        {
            this.demoIndex = demoIndex;
            this.leafNum = leafNum;
            this.leafColor = leafColor;
            this.leafAngle = leafAngle;
            this.trunkColor = trunkColor;
            this.trunkAngle = trunkAngle;
            this.trunkHeight = trunkHeight;
            this.iterateNum = iterateNum;

            this.clearChildren();

            int iteratetime = Int32.Parse(this.iterateNum);

            this.addChildren(setGroup());
            Group[] groupArr = new Group[iteratetime];
            groupArr = setGroupArr();
            foreach (Group tempgroup in groupArr)
            {
                this.addChildren(tempgroup);
            }
        }
    }
    #endregion

}
